﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ecdl.demo.model.expression;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;

using System.Xml.Serialization;
using System.Xml;

namespace ecdl.demo.model.expression {
    /// <summary>
    /// An expression that sets the context to Document.MainDocumentPart.HeaderParts
    /// </summary>
    public class HeaderSelector : Selector {

        public HeaderSelector() {
            AddSelector(new TypedSelector<WordprocessingDocument, OpenXmlElement>(GetAllHeaders));
        }

        private IEnumerable<OpenXmlElement> GetAllHeaders(WordprocessingDocument document) {
            return GetHeadersFromDocumentPart(document.MainDocumentPart);
        }

        private IEnumerable<OpenXmlElement> GetHeadersFromDocumentPart(MainDocumentPart part) {
            var headers = new List<OpenXmlElement>();
            foreach (var headerPart in part.HeaderParts) {
                headers.Add(headerPart.RootElement);
            }
            return headers;
        }
    }

    //TODO register at generic-serializer
    public class HeaderSelectorDTO : SingleExpressionDTO {

        public HeaderSelectorDTO():base(typeof(HeaderSelector)) {
        }
    }
}
